﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Slides.Pptx;
using System.Drawing;
using Aspose.Slides.Export;
using Aspose.Slides.Effects;
using System.IO;
using Aspose.Slides;
using System.Drawing.Imaging;

namespace PPTUtilities
{
    class PPTText
    {
        static HyperlinkEx HLink = null;
        public static void CreateTextBox(PresentationEx pres, int slideposition, int Xposition, int Yposition, int width, int Height, int fontsize, Color col, string text, TextAlignmentEx alignEx, bool FontBold)
        {
            //Get the first slide
            SlideEx sld = (SlideEx)pres.Slides[slideposition];

            //Add an AutoShape of Rectangle type
            sld.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, Xposition, Yposition, width, Height);
            AutoShapeEx ashp = sld.Shapes.OfType<AutoShapeEx>().Last();
            ashp.AddTextFrame(text);
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillTypeEx.Solid;
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color = col;
            ashp.TextFrame.Paragraphs[0].ParagraphFormat.Alignment = alignEx;
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontBold = FontBold ? NullableBool.True : NullableBool.False;
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = fontsize;
            //Change the line color of the rectangle to White
            ashp.ShapeStyle.LineColor.Color = System.Drawing.Color.White;

            //Remove any fill formatting in the shape
            ashp.FillFormat.FillType = FillTypeEx.NoFill;
        }
        public static void CreateTextBox(PresentationEx pres, int slideposition, int Xposition, int Yposition, int width, int Height, int fontsize, List<TextProperties> prop, TextAlignmentEx alignEx, bool FontBold)
        {
            //Get the first slide
            SlideEx sld = (SlideEx)pres.Slides[slideposition];
            PortionEx newPort = null;
            //Add an AutoShape of Rectangle type
            sld.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, Xposition, Yposition, width, Height);
            AutoShapeEx ashp = sld.Shapes.OfType<AutoShapeEx>().Last();
            ParagraphEx para = ashp.TextFrame.Paragraphs[0];
            ashp.TextFrame.Paragraphs[0].ParagraphFormat.Alignment = alignEx;
            PortionEx port = para.Portions[0];
            port.Text = prop.First().Text;
            port.PortionFormat.FillFormat.FillType = FillTypeEx.Solid;
            port.PortionFormat.FillFormat.SolidFillColor.Color = prop.First().Forecolor;
            port.PortionFormat.FontBold = NullableBool.True; //To bold it
            port.PortionFormat.FontHeight = fontsize;
            for (int i = 1; i < prop.Count; i++)
            {

                newPort = new PortionEx(port);
                newPort.Text = prop[i].Text;
                newPort.PortionFormat.FillFormat.FillType = FillTypeEx.Solid;
                newPort.PortionFormat.FillFormat.SolidFillColor.Color = prop[i].Forecolor;
                para.Portions.Add(newPort);
            }
            ashp.FillFormat.FillType = FillTypeEx.NoFill;
        }

        public static void CreateTextBoxWithHyperlink(PresentationEx pres, int slideposition, int Xposition, int Yposition, int width, int Height, int fontsize, Color col, string text, string tooltip, TextAlignmentEx exAlign)
        {
            SlideEx slide = (SlideEx)pres.Slides[slideposition];
            slide.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, Xposition, Yposition, width, Height);
            AutoShapeEx ashp = slide.Shapes.OfType<AutoShapeEx>().Last();
            ashp.AddTextFrame(text);
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillTypeEx.Solid;
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color = col;
            ashp.TextFrame.Paragraphs[0].ParagraphFormat.Alignment = exAlign;
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontBold = NullableBool.True;
            ashp.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = fontsize;
            ashp.ShapeStyle.LineColor.Color = System.Drawing.Color.White;
            ashp.FillFormat.FillType = FillTypeEx.NoFill;
            ashp.TextFrame.Paragraphs[0].Portions[0].Text = text;
            HyperlinkEx HLink = new HyperlinkEx(pres.Slides[2]);
            //HyperlinkEx HLink = new HyperlinkEx(new HyperlinkEx(pres.Slides[1]), "", tooltip, false, false, false);
            ashp.TextFrame.Paragraphs[0].Portions[0].HLinkClick = HLink;
        }
        public static void CreateBulletList(PresentationEx pres, int slideposition, int Xposition, int Yposition, int width, int Height, List<string> ListItems, int fontsize, Color ForeCol, Color BackCol, TextAlignmentEx alignEx, bool isfontBold, int screenPos)
        {
            SlideEx sld = (SlideEx)pres.Slides[slideposition];
            sld.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, Xposition, Yposition, width, Height);
            AutoShapeEx rect = sld.Shapes.OfType<AutoShapeEx>().Last();
            //rect.LineFormat. = LineDashStyleEx.;
            //Add a text frame
            rect.AddTextFrame("");
            rect.TextFrame.AutofitType = TextAutofitTypeEx.Shape;
            rect.TextFrame.WrapText = true;
            rect.FillFormat.FillType = FillTypeEx.Solid;
            rect.FillFormat.SolidFillColor.Color = BackCol;

            ParagraphEx para = rect.TextFrame.Paragraphs[0];
            para.Text = ListItems[0];
            para.ParagraphFormat.BulletType = BulletTypeEx.Symbol;
            para.ParagraphFormat.Alignment = alignEx;
            //para.ParagraphFormat.BulletChar = 1;
            ParagraphEx newPara = null;
            for (int i = 0; i < ListItems.Count; i++)
            {
                newPara = new ParagraphEx(para);
                newPara.Text = ListItems[i];
                newPara.Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                newPara.Portions[0].FillFormat.SolidFillColor.Color = ForeCol;
                newPara.ParagraphFormat.Alignment = alignEx;
                newPara.ParagraphFormat.Indent = 30;
                newPara.ParagraphFormat.SpaceAfter = 30;
                newPara.Portions[0].PortionFormat.FontHeight = fontsize;
                newPara.Portions[0].PortionFormat.FontBold = isfontBold ? NullableBool.True : NullableBool.False;
                HLink = new HyperlinkEx(pres.Slides[Convert.ToInt16(screenPos + i + 2)]);
                //HyperlinkEx HLink = new HyperlinkEx(new HyperlinkEx(pres.Slides[1]), "", tooltip, false, false, false);
                newPara.Portions[0].HLinkClick = HLink;
                //newPara.ParagraphFormat.BulletChar = Convert.ToChar(118);
                rect.TextFrame.Paragraphs.Add(newPara);
            }
        }

        public static void CreateMediaBulletList(PresentationEx pres, int slideposition, int Xposition, int Yposition, int width, int Height, List<string> ListItems, int fontsize, Color ForeCol, Color BackCol, TextAlignmentEx alignEx, bool isfontBold, Dictionary<string,int> screenPos)
        {
            SlideEx sld = (SlideEx)pres.Slides[slideposition];
            sld.Shapes.AddAutoShape(ShapeTypeEx.Rectangle, Xposition, Yposition, width, Height);
            AutoShapeEx rect = sld.Shapes.OfType<AutoShapeEx>().Last();
            //rect.LineFormat. = LineDashStyleEx.;
            //Add a text frame
            rect.AddTextFrame("");
            rect.TextFrame.CenterText = true;
            rect.TextFrame.AutofitType = TextAutofitTypeEx.Shape;
            rect.TextFrame.WrapText = true;
            rect.FillFormat.FillType = FillTypeEx.Solid;
            rect.FillFormat.SolidFillColor.Color = BackCol;

            ParagraphEx para = rect.TextFrame.Paragraphs[0];
            para.ParagraphFormat.SpaceAfter = 1;
            para.ParagraphFormat.SpaceBefore = 1;
            para.ParagraphFormat.BulletType = BulletTypeEx.Symbol;
            para.ParagraphFormat.Alignment = alignEx;
            //para.ParagraphFormat.BulletChar = 1;
            ParagraphEx newPara = null;
            int slidePos = 0;
            for (int i = 0; i < ListItems.Count; i++)
            {
                newPara = new ParagraphEx(para);
                newPara.Text = ListItems[i];
                slidePos = screenPos.Where(x => x.Key == ListItems[i]).FirstOrDefault().Value;
                newPara.Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                newPara.Portions[0].FillFormat.SolidFillColor.Color = ForeCol;
                newPara.ParagraphFormat.Alignment = alignEx;
                newPara.ParagraphFormat.Indent = 15;
                newPara.Portions[0].PortionFormat.FontHeight = fontsize;
                newPara.Portions[0].PortionFormat.FontBold = isfontBold ? NullableBool.True : NullableBool.False;
                HLink = new HyperlinkEx(pres.Slides[slidePos]);
                newPara.Portions[0].HLinkClick = HLink;
                rect.TextFrame.Paragraphs.Add(newPara);
            }
                //if ((yyy + 1)<ListItems.Count)
                //{
                //    PortionEx por = new PortionEx();
                //    por.PortionFormat.Spacing = 10;
                //    por.Text = ListItems[yyy + 1];
                //    por.FillFormat.FillType = FillTypeEx.Solid;
                //    por.FillFormat.SolidFillColor.Color = ForeCol;
                //    por.PortionFormat.FontHeight = fontsize;
                //    por.PortionFormat.FontBold = isfontBold ? NullableBool.True : NullableBool.False;
                //    newPara.Portions.Add(por);
                //    newPara.Portions[1].HLinkClick = HLink;
                //}
        }
    }
    class TextProperties
    {
        public Color Forecolor { get; set; }
        public string Text { get; set; }
    }
}

